package com.soft.bams.entity;

import com.soft.bams.exception.BalanceNotEnoughException;


/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/17
 */
// 信用账户
public class CreditAccount extends Account {
    private double ceiling;

    public CreditAccount() {
        super();
    }

    public CreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling) {
        super(id, password, name, personId, email, balance);
        this.ceiling = ceiling;
    }

    // 重写取款方法，允许透支
    @Override
    public Account withdraw(double amount) throws BalanceNotEnoughException {
        if ((getBalance() + ceiling) >= amount) {
            if(getBalance() >= amount){
                setBalance(getBalance() - amount);
            }else {
                ceiling -= amount - getBalance();
                setBalance(0.0);
            }
        }else {
            throw new BalanceNotEnoughException("您的余额不足");
        }
        return this;
    }

    public double getCeiling() {
        return ceiling;
    }

    public void setCeiling(double ceiling) {
        this.ceiling = ceiling;
    }

    @Override
    public String toString() {
        return "CreditAccount{" +
                "id=" + getId() +
                ", password='" + getPassword() + '\'' +
                ", name='" + getName() + '\'' +
                ", personId='" + getPersonId() + '\'' +
                ", email='" + getEmail() + '\'' +
                ", balance=" + getBalance() +
                ", ceiling=" + ceiling +
                '}';
    }
}
